import React, {
    Component
} from 'react';
import {
    Text,
    FlatList,
    RefreshControl,
    View,
    StyleSheet,
    Image,
    Alert,
    ActionSheetIOS,
    AlertIOS,
    InteractionManager,
    TouchableOpacity,
    NativeModules
} from 'react-native';
import NetUtil from '../../common/NetUtil';
import config from '../../config/config';
import commonStyles from '../../styles/commonstyles';

import {getProductList, delProduct, updateSoldOut} from './actions';

import ProductListItem from './productListItem';

import {connect} from 'react-redux';

import {naviPush} from '../../navigatorApp/actions'

class ProductList extends Component {
    constructor(props) {
        super(props);
        this.productstate = props.state;
        this.page = 1;
        this.state = {
            isLoading: false,
            refreshing: false,
            footText: "",
            hasMore: false,
        };
    }

    componentDidMount() {
        InteractionManager.runAfterInteractions(() => {
            this.setState({refreshing: true})
            this.getProductList();
        });
    }

    /**
     * -2:已退回;-1:待提交;0:待审核;1:审核通过
     */
    getProductList = () => {
        const self = this;
        let params = {
            token: this.props.token,
            catCode: this.props.catCode,
            page: this.page,
            size: 5
        };

        if (this.productstate == '1') {
            params.isOnSale = 1;
            params.hasAduited = '1';
        } else if (this.productstate == '0') {
            params.isOnSale = 0;
            params.hasAduited = '1';
        } else if (this.productstate == '3'){
            params.hasAduited = '-2,-1';
        }else{
            params.hasAduited = '0';
        }

        this.props.getProductList(params, this.productstate,
            function (params) {
                self.setState({
                    refreshing: false,
                    isLoading: false,
                    hasMore: params.hasMore,
                    footText: params.footText,
                })
            });
    }

    onProductAction = (type, id) => {
        let params = {
            token: this.props.token,
            goodId: id,
        };

        if (type == "上架") {
            params.isOnSale = 1
        } else {
            params.isOnSale = 0
        }

        const self = this;

        updateSoldOut(params, () => {
            self.page = 1;
            self.getProductList();
        })
    }

    delProduct = (id) => {
        let params = {
            token: this.props.token,
            id: id,
        }

        const self = this;

        Alert.alert('提示',
            "确定删除？",
            [
                {
                    text: '取消', onPress: () => {
                }
                },
                {
                    text: '确定', onPress: () => {
                    delProduct(params, () => {
                        self.page = 1;
                        self.getProductList();
                    })
                }
                },
            ]);

    }

    _renderItem = ({item}) => {
        return <View>
            <ProductListItem
                onProductAction={(type, id) => {
                    this.onProductAction(type, id)
                }}
                delProduct={(id) => {
                    this.delProduct(id)
                }}
                onPress={() => this.gotoDetail(item)}
                productstate={this.productstate}
                product={item}
            />
        </View>
    }

    _sepa = () => {
        return (<View style={{height: 10, backgroundColor: '#eee'}}></View>)
    }

    _renderFooter = () => {
        return (
            <View style={{
                padding: 10,
                borderTopColor: "#eee",
                borderTopWidth: 1,
                flex: 1,
                flexDirection: 'row',
                justifyContent: 'center'
            }}>
                <Text style={{color: "gray"}}>{this.state.footText}</Text>
            </View>
        )
    }

    _onEndReached = () => {
        if (this.state.hasMore && !this.state.isLoading) {
            this.page = this.page + 1;
            this.setState({isLoading: true});
            this.getProductList();
        }
    }

    _onRefresh = () => {
        this.page = 1;
        this.setState({refreshing: true})
        this.getProductList();
    }

    gotoDetail = (item) => {
        this.props.naviPush("ProductAdd", {goodsId: item.goodsId, catId: this.props.catId})
    };

    render() {

        let productList = [];
        if (this.productstate == '1') {
            productList = this.props.productOnSale.list;
        } else if (this.productstate == '0') {
            productList = this.props.productOutSale.list;
        } else if (this.productstate == '3') {
            productList = this.props.productInEdit.list;
        } else {
            productList = this.props.productInCheck.list;
        }

        return <View style={{flex: 1}}>
            <FlatList
                style={{flex: 1}}
                ItemSeparatorComponent={this._sepa}
                data={productList}
                renderItem={this._renderItem}
                ListFooterComponent={this._renderFooter}
                onEndReached={this._onEndReached}
                onEndReachedThreshold={0}
                onRefresh={this._onRefresh}
                refreshing={this.state.refreshing}
            />
        </View>
    }
}

const mapStateToProps = (state) => {
    return {
        token: state.mainmodel.token,
        productOnSale: state.product.productOnSale,
        productOutSale: state.product.productOutSale,
        productInCheck: state.product.productInCheck,
        productInEdit: state.product.productInEdit,
    }
}

const mapDispatchToProps = (dispatch) => {
    return {
        getProductList: (token, page, productstate, callback) => {
            dispatch(getProductList(token, page, productstate, callback))
        },
        naviPush: (routerName, params) => {
            dispatch(naviPush(routerName, params));
        }
    }
}

export default connect(
    mapStateToProps,
    mapDispatchToProps,
)(ProductList);
